from django.db import models
import datetime


# Create your models here.

# 科室表
class UnitType(models.Model):
    name = models.CharField(verbose_name='科室类型', max_length=12, default='')

    class Meta:
        verbose_name = '科室类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class UserUnit(models.Model):
    code = models.CharField(verbose_name='科室代码', max_length=12, primary_key=True)
    name = models.CharField(verbose_name='科室名称', max_length=30)
    type = models.ForeignKey(UnitType, verbose_name='科室类型', on_delete=models.SET_NULL, null=True)

    class Meta:
        verbose_name = '科室'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class UserRole(models.Model):
    name = models.CharField(verbose_name='角色名称', max_length=12, default='')

    class Meta:
        verbose_name = '用户角色'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class UserInfo(models.Model):
    username = models.CharField(verbose_name='用户账号', max_length=12, null=False, unique=True)
    role = models.ForeignKey(UserRole, verbose_name='角色', on_delete=models.SET_NULL, null=True)
    name = models.CharField(verbose_name='用户姓名', unique=True, max_length=10, default='')
    mobile = models.CharField(verbose_name='手机号', max_length=20)
    avatar = models.ImageField(verbose_name='用户头像', max_length=200, upload_to='assets_users/avatar/%Y/%m', null=True,
                               blank=True)
    gender = models.CharField(verbose_name='性别', choices=(('1', '男'), ('0', '女')), default='male',
                              max_length=10)
    password = models.CharField(verbose_name='密码', max_length=20, default='123456')
    position = models.ForeignKey(UserUnit, verbose_name='科室', on_delete=models.SET_NULL, blank=True, null=True)
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=((1, '正常'), (2, '停用')), default=1)
    stop_time = models.DateTimeField(verbose_name='停用时间', blank=True, null=True)

    class Meta:
        verbose_name = '用户详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 获取账户添加时间
    def get_join_days(self):
        join_times = datetime.datetime.now()
        return join_times


class UserLoginInfo(models.Model):
    action = models.PositiveSmallIntegerField(verbose_name='动作', choices=((1, '登录'), (2, '注销')), default=1)
    user = models.ForeignKey(UserInfo, verbose_name='用户', on_delete=models.CASCADE)
    ip = models.GenericIPAddressField(verbose_name='IP地址')
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    class Meta:
        verbose_name = '用户登录信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s - %s - %s" % (self.user, self.add_time, self.ip)
